package cn.xorange.commons.system.controller.platform;

import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.core.exception.SysException;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.system.entity.platform.SysProductSpu;
import cn.xorange.commons.system.service.platform.ISysProductSpuService;
import cn.xorange.commons.core.constants.SysConstants;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author : yangjian
 * date : 2024/9/13
 * description :
 */
@RestController
@Authorize(hasPerm = "platform:spu")
@RequestMapping("/platform/spu")
public class SysProductSpuController extends DataController<SysProductSpu, ISysProductSpuService> {

    @Authorize(hasPerm = "list")
    @GetMapping("/options")
    public ResultBean options(SysProductSpu productSpu){
        productSpu.selectColumn("spuId","spuName");
        productSpu.setStatus(SysConstants.FLAG_SUCCESS);
        return ResultBean.onSuccess(this.service.getList(productSpu));
    }

    @Authorize(hasPerm = "edit")
    @PutMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean update(@Validated @RequestBody SysProductSpu spu){
        if (Objects.isNull(spu.getSpuId())){
            return ResultBean.onFail(SysException.paramException());
        }
        if(SysConstants.FLAG_DISABLE.equals(spu.getStatus())){
            service.checkProductBySpuId(spu.getSpuId());
        }
        return ResultBean.onSuccess(this.service.update(spu));
    }

    @Authorize(hasPerm = "remove'")
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean del(@PathVariable Long id) {
        service.checkProductBySpuId(id);
        this.service.deleteById(id);
        return ResultBean.onSuccess();
    }
}
